package com.yhb.controller;

import com.yhb.service.AppPayService;
import me.edzh.spring.bootstrap.controller.BaseController;
import me.edzh.spring.bootstrap.model.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/callback")
public class CallBackController extends BaseController {

    @Autowired
    private AppPayService appPayService;

    @PostMapping("/aliPayPurchase")
    public void alipayCallback(HttpServletRequest request, HttpServletResponse response) {
        Map<String, String> params = new HashMap<>();
        Enumeration<String> e = request.getParameterNames();
        while (e.hasMoreElements()) {
            String key = e.nextElement();
            params.put(key, request.getParameter(key));
        }
        appPayService.onCallback(params);
        writeToClient(response, "ok");
    }
}
